﻿using System;
using System.Xml;

namespace Graphe.Collations
{
    public class Manuscript : ICollationElement, System.IComparable<Manuscript>
    {
        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public Manuscript() { }

        /// <summary>
        /// Constructor for fully initializing a manuscript.
        /// </summary>
        /// <param name="symbol">The symbol for the manuscript.</param>
        /// <param name="signifier">The alternative signifier for showing corrector or original.</param>
        public Manuscript(string symbol, string signifier)
        {
            Symbol = symbol;
            Signifier = signifier;
        }

        #endregion
        #region Private Instance Fields

        private string _signifier;
        private string _symbol;

        #endregion
        #region Public Properties

        /// <summary>
        /// Any extra signifier for the manuscript. Usually for identifying correctors.
        /// </summary>
        public string Signifier
        {
            get { return _signifier; }
            set
            {
                if (value == String.Empty)
                    return;

                _signifier = value;
            }
        }

        /// <summary>
        /// The shorthand symbol used to identify the manuscript.
        /// </summary>
        public string Symbol
        {
            get { return _symbol; }
            set
            {
                if (value == String.Empty)
                    return;

                _symbol = value;
            }
        }

        #endregion
        #region ICollationElement Members

        public void Parse(XmlReader reader, CollationDebugMessages debugMessages)
        {
            if (reader.MoveToFirstAttribute())
            {
                do
                {
                    ReadAndAssignAttribute(reader, debugMessages);
                }   while (reader.MoveToNextAttribute());
            }

            reader.Read();
            Symbol = reader.ReadContentAsString().Trim();

            //TODO: This is for reading my older collation files. When they are completely converted, remove.
            if (reader.Name == "Corrector")
            {
                reader.Read();
                Signifier = reader.ReadContentAsString().Trim();
            }
        }

        private void ReadAndAssignAttribute(XmlReader reader, CollationDebugMessages debugMessages)
        {
            if (reader.Name == "Signifier")
            {
                Signifier = reader.ReadContentAsString();
            }
            else
            {
                debugMessages.Add(String.Format("Attribute '{0}' with value '{1}' is unknown and will not be persisted in the in-memory model. This also means it won't be saved after you change the collation.", reader.Name, reader.ReadContentAsString()));
            }
        }

        public void Render(XmlTextWriter writer)
        {
            writer.WriteStartElement("Manuscript");

            if (!String.IsNullOrEmpty(Signifier))
            {
                writer.WriteAttributeString("Signifier", Signifier);
            }

            writer.WriteString(Symbol);
            writer.WriteEndElement();
        }

        #endregion
        #region IComparable<Manuscript> Members

        public int CompareTo(Manuscript other)
        {
            if (Symbol == other.Symbol && Signifier == other.Signifier)
                return 0;
            else
                return -1;
        }

        #endregion
    }
}
